<?php

################################################################################
##                                  Constants                                 ##
################################################################################

    define('SERUM_LIB_PATH',    dirname(__FILE__) . DIRECTORY_SEPARATOR .
                                '..' . DIRECTORY_SEPARATOR .
                                'lib' . DIRECTORY_SEPARATOR);

    define('PROJECT_MODULE_PATH', dirname(__FILE__) . DIRECTORY_SEPARATOR .
                                '..' . DIRECTORY_SEPARATOR .
                                '..' . DIRECTORY_SEPARATOR .
                                'project' . DIRECTORY_SEPARATOR .
                                'app' . DIRECTORY_SEPARATOR .
                                'modules' . DIRECTORY_SEPARATOR);
                 
################################################################################
##                                  Functions                                 ##
################################################################################

    require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Main.php';
    require_once SERUM_LIB_PATH . 'Functions' . DIRECTORY_SEPARATOR . 'Core.php';

    /**
     * Setting error reporting
     */
    error_reporting(E_ALL | E_STRICT);
    ini_set('display_startup_errors', 1);
    ini_set('display_errors', 1);

    set_include_path(get_include_path() . PATH_SEPARATOR . SERUM_LIB_PATH);

################################################################################
##                                 Main class                                 ##
################################################################################

    /**
     * Module Builder
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    class Builder
    {

    ////////////////////////////////////////////////////////////////////////////
    //                                Variables                               //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * The module's name
         *
         * @var string
         */
        private static $_name = '';

        /**
         * The package's name
         *
         * @var string
         */
        private static $_package = '';

    ////////////////////////////////////////////////////////////////////////////
    //                             Static Methods                             //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Runs the program
         *
         * @param       string      $_SERVER['argv'][1] : the module's name
         * @return      void
         */
        public static function main()
        {
            self::$_name = '';

            if (isset($_SERVER['argv'][1])) {
                self::$_name = $_SERVER['argv'][1];
            }

            if (self::$_name !== '') {
                self::$_name    = ucfirst(strtolower(self::$_name));

                if (in_array(substr(self::$_name, -1), array('s', 'x'))) {
                    self::$_package = self::$_name;
                } else if (substr(self::$_name, -1) == 'y') {
                     self::$_package = substr(self::$_name, 0, -1) .'ies';
                } else {
                    self::$_package = self::$_name .'s';
                }

                self::buildTree();
                self::buildFiles();
            } else {
                echo PHP_EOL . 'USAGE:';
                echo PHP_EOL . '  php Builder.php <module>';
                echo PHP_EOL;
            }

            exit(1);
        }

        /**
         * Builds the module's tree
         *
         * @return      void
         */
        private static function buildTree()
        {
            Core_IO_Directory::createDirectory(Core_IO_Path::combine(PROJECT_MODULE_PATH, self::$_package));
            Core_IO_Directory::createDirectory(Core_IO_Path::combine(PROJECT_MODULE_PATH, self::$_package, 'Classes'));
            Core_IO_Directory::createDirectory(Core_IO_Path::combine(PROJECT_MODULE_PATH, self::$_package, 'Classes', 'Db'));
            Core_IO_Directory::createDirectory(Core_IO_Path::combine(PROJECT_MODULE_PATH, self::$_package, 'controllers'));
            Core_IO_Directory::createDirectory(Core_IO_Path::combine(PROJECT_MODULE_PATH, self::$_package, 'controllers', 'admin'));
            Core_IO_Directory::createDirectory(Core_IO_Path::combine(PROJECT_MODULE_PATH, self::$_package, 'controllers', 'front'));
            Core_IO_Directory::createDirectory(Core_IO_Path::combine(PROJECT_MODULE_PATH, self::$_package, 'locales'));
            Core_IO_Directory::createDirectory(Core_IO_Path::combine(PROJECT_MODULE_PATH, self::$_package, 'locales', 'admin'));
            Core_IO_Directory::createDirectory(Core_IO_Path::combine(PROJECT_MODULE_PATH, self::$_package, 'locales', 'front'));
            Core_IO_Directory::createDirectory(Core_IO_Path::combine(PROJECT_MODULE_PATH, self::$_package, 'Setup'));
        }

        /**
         * Generates the module's classes
         *
         * @return      void
         */
        private static function buildFiles()
        {
            // Generate admin controller
            $path = Core_IO_Path::combine(PROJECT_MODULE_PATH, self::$_package, 'controllers', 'admin', 'IndexController.php');

            $content = '<?php' . PHP_EOL;
            $content .= PHP_EOL;
            $content .= '    require_once Core_IO_Path::combine(SERUM_MODULE_PATH, \'Base\', \'controllers\', \'admin\', \'IndexController.php\');'. PHP_EOL;
            $content .= PHP_EOL;
            $content .= '    /**'. PHP_EOL;
            $content .= '     * @package      Modules'. PHP_EOL;
            $content .= '     * @subpackage   '. self::$_package .'_Controllers_Admin'. PHP_EOL;
            $content .= '     *'. PHP_EOL;
            $content .= '     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html'. PHP_EOL;
            $content .= '     */'. PHP_EOL;
            $content .= '     class '. self::$_package .'_Controllers_Admin_Index extends Base_Controllers_Admin_Index'. PHP_EOL;
            $content .= '     {'. PHP_EOL;
            $content .= PHP_EOL;
            $content .= '    ////////////////////////////////////////////////////////////////////////////'. PHP_EOL;
            $content .= '    //                                Variables                               //'. PHP_EOL;
            $content .= '    ////////////////////////////////////////////////////////////////////////////'. PHP_EOL;
            $content .= PHP_EOL;
            $content .= '        /**'. PHP_EOL;
            $content .= '         * Model class'. PHP_EOL;
            $content .= '         *'. PHP_EOL;
            $content .= '         * @var string'. PHP_EOL;
            $content .= '         */'. PHP_EOL;
            $content .= '        protected $_model = \'Db'. self::$_name .'\';'. PHP_EOL;
            $content .= '    }'. PHP_EOL;

            Core_IO_File::writeAll($path, $content);

            // Generate admin locale files
            $path = Core_IO_Path::combine(PROJECT_MODULE_PATH, self::$_package, 'locales', 'admin', 'en_US.csv');

            $content = '"title";"'. self::$_package .'"'. PHP_EOL;
            $content = '"list_title";"'. self::$_package .'"'. PHP_EOL;
            $content .= '"side_title";"'. self::$_package .'"'. PHP_EOL;
            Core_IO_File::writeAll($path, $content);

            $path = Core_IO_Path::combine(PROJECT_MODULE_PATH, self::$_package, 'locales', 'admin', 'fr_FR.csv');
            Core_IO_File::writeAll($path, $content);

            // Generate setup file
            $path = Core_IO_Path::combine(PROJECT_MODULE_PATH, self::$_package, 'Setup', 'Installer.php');

            $content = '<?php' . PHP_EOL;
            $content .= PHP_EOL;
            $content .= '    /**'. PHP_EOL;
            $content .= '     * @package      Modules'. PHP_EOL;
            $content .= '     * @subpackage   '. self::$_package .'_Setup'. PHP_EOL;
            $content .= '     *'. PHP_EOL;
            $content .= '     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html'. PHP_EOL;
            $content .= '     */'. PHP_EOL;
            $content .= '     class '. self::$_package .'_Setup_Installer extends Modules_Setup_Installer'. PHP_EOL;
            $content .= '     {'. PHP_EOL;
            $content .= PHP_EOL;
            $content .= '     }'. PHP_EOL;

            Core_IO_File::writeAll($path, $content);

            // Generate configuration file
            $path = Core_IO_Path::combine(PROJECT_MODULE_PATH, self::$_package, 'config.ini.php');

            $content = ';<?php die(\'\'); ?>' . PHP_EOL;
            $content .= '; for security reasons , don\'t remove or modify the first line'. PHP_EOL;
            $content .= PHP_EOL;
            $content .= '; Production site configuration data'. PHP_EOL;
            $content .= '[production]'. PHP_EOL;
            $content .= PHP_EOL;
            $content .= '; list of required module separated by \',\' or false if none'. PHP_EOL;
            $content .= 'requires           = Modules'. PHP_EOL;
            $content .= PHP_EOL;
            $content .= '; Staging site configuration data inherits from production and'. PHP_EOL;
            $content .= '; overrides values as necessary'. PHP_EOL;
            $content .= '[staging : production]'. PHP_EOL;

            Core_IO_File::writeAll($path, $content);

            // Generate database file
            $path = Core_IO_Path::combine(PROJECT_MODULE_PATH, self::$_package, 'models.yml');

            $content = '---' . PHP_EOL;
            $content .= PHP_EOL;
            $content .= '# TABLE '. strtoupper(self::$_name) . PHP_EOL;
            $content .= '########################'. PHP_EOL;
            $content .= 'Db'. self::$_name .':'. PHP_EOL;
            $content .= '  inheritance:'. PHP_EOL;
            $content .= '    extends: DbBase'. PHP_EOL;
            $content .= '  tableName: '. strtolower(self::$_package) .'_'. strtolower(self::$_name) . PHP_EOL;
            $content .= '  columns:'. PHP_EOL;

            Core_IO_File::writeAll($path, $content);
        }
    }

################################################################################
##                                Main program                                ##
################################################################################

    Builder::main();